#!/usr/bin/env python
# -*- conding: utf-8 -*-

"""
@Time     : 2024/9/5 6:43
@Author   : liujingmao
@File     : 3.递归JSON分割器示例.py
"""
import json

import requests
from langchain_text_splitters import RecursiveJsonSplitter

# 1. get and load json file
url = "https://api.smith.langchain.com/openapi.json"
json_data = requests.get(url).json()
print(json_data)
print(len(json.dumps(json_data)))

# 2. 递归Json分割器
text_split = RecursiveJsonSplitter(max_chunk_size=300)

# 3. 分割Json数据并创建文档
json_chunks = text_split.split_json(json_data)
chunks = text_split.create_documents(json_chunks)

# 4. 输出内容
count = 0
for chunk in chunks:
    count += len(chunk.page_content)

print(count)
